package com.example.dao.imol;

import com.example.dao.BookDao;
import com.example.entity.Book;
<<<<<<< HEAD
import java.util.logging.Level;
import java.util.logging.Logger;
=======

>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/library_management?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "root123";

<<<<<<< HEAD

    @Override
    public void addBook(Book book) {
        try {
        Class.forName("com.mysql.cj.jdbc.Driver");
=======
    @Override
    public void addBook(Book book) {
>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "INSERT INTO books (book_id, book_name, author, publisher, publication_year, price, stock_quantity) VALUES (?,?,?,?,?,?,?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, book.getBookId());
            statement.setString(2, book.getBookName());
            statement.setString(3, book.getAuthor());
            statement.setString(4, book.getPublisher());
            statement.setInt(5, book.getPublicationYear());
            statement.setDouble(6, book.getPrice());
            statement.setInt(7, book.getStockQuantity());
            statement.executeUpdate();
<<<<<<< HEAD
        }
        } catch (SQLException | ClassNotFoundException e) {
=======
        } catch (SQLException e) {
>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
            e.printStackTrace();
        }
    }

<<<<<<< HEAD


    private static final Logger LOGGER = Logger.getLogger(BookDaoImpl.class.getName());

=======
>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
    @Override
    public List<Book> getBooks(String keyword) {
        List<Book> bookList = new ArrayList<>();
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
<<<<<<< HEAD
            String sql = "SELECT * FROM books WHERE book_name LIKE ? OR author LIKE ? OR publisher LIKE ?";
=======
            String sql = "SELECT * FROM books WHERE book_name LIKE? OR author LIKE? OR publisher LIKE?";
>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, "%" + keyword + "%");
            statement.setString(2, "%" + keyword + "%");
            statement.setString(3, "%" + keyword + "%");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book(
                        resultSet.getString("book_id"),
                        resultSet.getString("book_name"),
                        resultSet.getString("author"),
                        resultSet.getString("publisher"),
                        resultSet.getInt("publication_year"),
                        resultSet.getDouble("price"),
                        resultSet.getInt("stock_quantity")
                );
                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bookList;
    }

<<<<<<< HEAD


=======
>>>>>>> b820b4c53617dce5ceda1c53c8071e1b57d2805e
    @Override
    public void updateBook(Book book) {
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "UPDATE books SET book_name=?, author=?, publisher=?, publication_year=?, price=?, stock_quantity=? WHERE book_id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, book.getBookName());
            statement.setString(2, book.getAuthor());
            statement.setString(3, book.getPublisher());
            statement.setInt(4, book.getPublicationYear());
            statement.setDouble(5, book.getPrice());
            statement.setInt(6, book.getStockQuantity());
            statement.setString(7, book.getBookId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteBook(String bookId) {
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "DELETE FROM books WHERE book_id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, bookId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}